package com.hopu.controller;


import com.aliyun.oss.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopu.pojo.R;
import com.hopu.pojo.Role;
import com.hopu.pojo.Update;
import com.hopu.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author YwaiX
 * @since 2024-09-12
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Cacheable("selectAll")
    @GetMapping("selectAll")
    public R selectAll(Page<Role> page, Role role) {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isNullOrEmpty(role.getRoleName())){
            wrapper.like(Role::getRoleName, role.getRoleName());
        }
        return R.ok(this.roleService.page(page, wrapper));
    }

    @PutMapping("update")
    public R update(@Validated(Update.class) Role role) {
        this.roleService.update(role);
        return R.ok("修改成功");
    }

    @PutMapping("updateStatus")
    public R updateStatus(@RequestBody Role role) {
        System.out.println(role.getId());
        System.out.println(role.getStatus());
        roleService.updateStatus(role);
        return R.ok("修改成功");
    }

    /**
     * 批量删除角色
     *
     * @param ids
     * @return
     */
    @GetMapping("/deleteRole/{ids}")
    public R deleteStaff(@PathVariable List<Integer> ids) {
        System.out.println(ids);
        roleService.deleteStaff(ids);
        return R.ok("操作成功");
    }


    /**
     * 添加数据
     *
     * @param role 实体对象
     * @return 修改结果
     */
    @PostMapping("insert")
    public R insert(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setStatus(1);
        return R.ok(this.roleService.add(role));
    }
    @GetMapping("getRoleById")
    public R getRoleById(@RequestParam("roleId") Integer roleId){
        return R.ok(this.roleService.getRoleById(roleId));
    }
}
